package cn.bdqn.or.controller.order;

import cn.bdqn.or.pagedata.TransactionPageData;
import cn.bdqn.or.pojo.Order;
import cn.bdqn.or.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 交易信息页面控制层
 */
@Controller
@RequestMapping("/transaction")
public class TransactionPageController {

    @Autowired
    @Qualifier("orderService")
    private OrderService orderService;

    @RequestMapping("/getAllOrder")
    public Map<String,Object> getAllOrder(){
        List<Order> list = orderService.queryAll();
        TransactionPageData transactionPageData = new TransactionPageData();
        int[] allOrder = new int[12];//所有订单
        int[] pendingPayment = new int[12];//待付款
        int[] paid = new int[12];//已付款
        int[] dropshipping = new int[12];//代发货
        for(Order order : list){
            int month = order.getOrderDate().getMonth()-1;
            if(order.getPayStatus() == 1){
                paid[month]++;//已付款
                transactionPageData.setAmountOfTheTransaction(new BigDecimal(transactionPageData.getAmountOfTheTransaction()).add(order.getSumPrice()).doubleValue());
            }
            if(order.getPayStatus() == 1 && order.getIssuanceStatus() == 1 && order.getReceivingStatus() == 1){
                transactionPageData.setSuccessfulTransaction(transactionPageData.getSuccessfulTransaction()+1);
            }
            if(order.getPayStatus() == 2 || order.getIssuanceStatus() == 2 || order.getReceivingStatus() == 2){
                transactionPageData.setTransactionFailed(transactionPageData.getTransactionFailed()+1);
            }
            if(order.getRefundStatus() == 1){
                transactionPageData.setRefundAmount(new BigDecimal(transactionPageData.getRefundAmount()).add(order.getSumPrice()).doubleValue());
            }
            allOrder[month]++;//所有订单
            if (order.getPayStatus() == 0){
                pendingPayment[month]++;//待付款
            }
            if(order.getIssuanceStatus() == 0){
                dropshipping[month]++;//代发货
            }
        }
        transactionPageData.setQuantityOfOrder(list.size());
        Map<String,Object> map = new HashMap();
        map.put("transactionPageData",transactionPageData);
        map.put("allOrder",allOrder);
        map.put("pendingPayment",pendingPayment);
        map.put("paid",paid);
        map.put("dropshipping",dropshipping);
        return map;
    }

}
